/**   
* Copyright: Copyright (c) 2015 master
* 
* @ClassName: UserController.java
* @Description: 用户管理类
*
* @version: v1.0.0
* @author: xuyiping
* @date: 2015-4-20 上午9:37:24 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
*/
package com.byxy.controller;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.byxy.model.User;
import com.byxy.service.UserService;
import com.byxy.util.Constrants;
import com.byxy.util.ImageCode;
import com.byxy.util.Log4jLogger;
import com.byxy.util.PageQueryUtil;
import com.byxy.util.SystemUtil;

@Controller
/*@Module("UserSysManager")*/
public class UserController extends BaseController {
	private static final Log4jLogger log = Log4jLogger
			.getLogger(UserController.class);
	
	@Resource
	protected UserService userService;

	/*@Resource
	protected PrivilegeService prevService;

	@Resource
	protected DepartmentService dpService;*/
	
	/**   
	* @Function: login
	* @Description: 用户登录
	*
	* @param:参数描述
	* @return：返回结果描述
	* @throws：异常描述
	*
	* @version: v1.0.0
	* @author: xuyiping
	* @date: 2015-4-20 上午9:37:24
	*
	* Modification History:
	* Date         Author          Version            Description
	*---------------------------------------------------------*
	* 2014-1-14     xuyiping           v1.0.0               修改原因
	*/
	/*@RequestMapping("/admin_index")
	public String login() throws Exception {
		try {
			getSession().removeAttribute(Constrants.USER_KEY);
			return "user/login";
		} catch (Exception e) {
			log.error("user redirect login error:", e);
			throw e;
		}
	}*/
	
	/**
	 * 
		* @Function: validPic
		* @Description: 验证码 
		*
		* @param:参数描述
		* @return：返回结果描述
		* @throws：异常描述
		*
		* @version: v1.0.0
		* @author: master
		* @date: 2015-5-19   下午5:22:39
		*
		* Modification History:
		* Date         Author          Version            Description
		*---------------------------------------------------------*
	 */
	@RequestMapping("/check_code")
	public void validPic(HttpServletResponse response) throws Exception{
		try {
			Map<String,BufferedImage> map = ImageCode.getImage(80, 42, 4,20);
			String checkCode = map.keySet().iterator().next();
			getSession().setAttribute("imageCode", checkCode);
			BufferedImage image = map.values().iterator().next();
			// 禁止图像缓存。  
			response.setHeader("Pragma", "no-cache"); 
			response.setHeader("Cache-Control", "no-cache"); 
			response.setDateHeader("Expires", 0); 
	  
			response.setContentType("image/jpeg"); 
	  
	         // 将图像输出到Servlet输出流中。  
	         ServletOutputStream sos = response.getOutputStream(); 
	         ImageIO.write(image, "jpeg", sos); 
	         sos.close(); 
		} catch (Exception e) {
			log.error("check code error:", e);
			throw e;
		}
	}
	
	/**
	 * 
		* @Function: loginOut
		* @Description: 用户注销
		*
		* @param:参数描述
		* @return：返回结果描述
		* @throws：异常描述
		*
		* @version: v1.0.0
		* @author: master
		* @date: 2015-5-18   下午5:26:11
		*
		* Modification History:
		* Date         Author          Version            Description
		*---------------------------------------------------------*
	 */
	@RequestMapping("/loginOut")
	public String loginOut() throws Exception {
		try {
			getSession().removeAttribute(Constrants.USER_KEY);
			getSession().invalidate();
			return "user/login";
		} catch (Exception e) {
			log.error("user  login out error:", e);
			throw e;
		}
	}
	
	
	/**
	 * 
		* @Function: loginDeal
		* @Description: 用户登录数据处理
		*
		* @param:参数描述
		* @return：返回结果描述
		* @throws：异常描述
		*
		* @version: v1.0.0
		* @author: master
		* @date: 2015-5-6   上午9:54:40
		*
		* Modification History:
		* Date         Author          Version            Description
		*---------------------------------------------------------*
		* 2014-1-14     xuyiping           v1.0.0               修改原因
	 */
	@RequestMapping("/login")
	public ModelAndView loginDeal(@RequestParam String em, @RequestParam String pd)  throws Exception{
		     
			ModelAndView modelAndView = new ModelAndView();
			User user = userService.findUser(em.trim(),
					pd.trim());
			if(user!=null){
				modelAndView.addObject("user", user);
				modelAndView.setViewName("page/success");
			}
			return modelAndView;
		
		}

	
}
